/**
 * User interface for vdjo.com
 */
var ui = {
	
	nextDisabled : [true, true],
	mixnowDisabled : true,
	
	/**
	 * Updates the buffer bar
	 * @param {Int} key (0-1)
	 * @param {Int} status (0-100)
	 */
	updateBuffer : function(key, status, start) {
		debug.print("ui.updateBuffer [key: " + key + ", status: " + status + ", start: " + start + "]");
		var id = "#vid" + key + "-slider-buffer";
		var width = parseInt(status * 1.87) + "px";
		var left = parseInt(start * 1.87) + "px";
		$(id).css("width", width).css("left", left);
	},
	
	updatePosition : function(key, position) {
		debug.print("ui.updatePosition [key: " + key + ", position: " + position + "]");
		$("#vid" + key + "-slider-bar").slider('option', 'value', position);
	},
	
	updateTime : function(key, time) {
		debug.print("ui.updateTime [key: " + key + ", time: " + time + "]");
		$("#vid" + key + "-time").text(time);
	},
	
	/**
	 * Updates the play/pause state of the vids in the ui
	 * @param {Int} key (0-1)
	 * @param {Status} status ('play', 'pause')
	 */
	updatePlayPause : function(key, status) {
		debug.print("ui.updatePlayPause");
		var id = "#vid" + key + "-pp-button:first, #vid" + key + "-title, #vid"+key+"-pp-button-fx";
		$(id).removeClass("play pause").addClass(status);
		$("#vid"+key+"-pp-button-fx").show().effect('puff', {}, 300);		
	},
	
	init : function() {
		debug.print("ui.init");
		this.initPositionSlider(0);
		this.initPositionSlider(1);
		this.initFader();
	},
	
	initPositionSlider : function(key) {
		debug.print("ui.initPositionSlider [key: " + key + "]");
		$("#vid" + key + "-slider-bar").slider({
			min: 0,
			max: 100,
			value: 0,
			start: function() {
				timer.skipPositionUpdates = true;
			},
			stop: function() {
				timer.skipPositionUpdates = false;
			},
			slide: function() {
				controller.positionChange(key);
				player[key].updateTime();
			}
		});
		$("#vid" + key + "-slider-bar").bind("mousedown", function() {
			controller.positionChange(key);
		});
	},
	
	initFader : function(key) {
		debug.print("ui.initFader");
		$("#fader-container").slider({
			min: 0,
			max: 100,
			value: 50,
			stop: function() {				
				controller.faderChange();
			},
			slide: function() {
				controller.faderChange();
			}
		});
	},
	
	setFader : function() {
		debug.print("ui.setFader");
	},
	
	setVideoStatus : function(key, state) {
		debug.print("ui.setVideoLoading [key: " + key + ", state: " + state + "]");
		$("#vid" + key + "-wrapper").removeClass("loading").removeClass("ready").removeClass("idle");
		if (state == "loading") {
			$("#vid" + key + "-wrapper").addClass("loading");
		} else if (state == "ready") {
			$("#vid" + key + "-wrapper").addClass("ready");
		} else if (state == "playing") {
			//$("#vid" + key + "-wrapper").addClass("idle");
		}
	},
	
	setVideoTitle : function(key, item) { 
		$("#vid"+key+"-title a").show();
		$("#vid"+key+"-title span").text(item.artist + " - " + item.title);
	},
	
	setWindowTitle : function(title) {
		document.title = title + ' - ' + config.title;
	},
	
	setSearchStatus : function(state) {
		debug.print("ui.setSearchStatus [state: " + state + "]");
		$("#search-loading, #search-next").removeClass("active");
		if (state == "loading") {
			$("#search-loading, #search-next").addClass("active");
		} else if (state == "idle") {
			$("#search-next").show();
		}
		$("#search-loading").show();
	},
	
	setAutodj : function(status) {
		debug.print("ui.setAutodj [status: " + status + "]");
		var bt = $("#button-autodj");
		if (status == true) {
			$("#autodj-controls").addClass("on");
			bt
				.removeClass('tmp-disabled')
				.addClass('tmp-enabled')
				.unbind("mouseout")
				.mouseout(function() { bt.removeClass('tmp-enabled').addClass('enabled'); });			
		} else {
			$("#autodj-controls").removeClass("on");
			bt
				.removeClass('tmp-enabled')
				.removeClass('enabled')
				.addClass('tmp-disabled')
				.unbind("mouseout")
				.mouseout(function() { bt.removeClass('tmp-disabled'); });
		}
	},
	
	updateAutodjControls : function() {
		debug.print("ui.updateAutodjControls");
		// disable next for 0?
		if (this.nextDisabled[0] && player[0].state != 1 && np.count() > 0) {
			$(".autodj-next0").removeClass("disabled");
			this.nextDisabled[0] = false;
		} else if (this.nextDisabled[0] == false && player[0].state == 1) {
			$(".autodj-next0").addClass("disabled");
			this.nextDisabled[0] = true;
		}
		// disable next for 1?
		if (this.nextDisabled[1] && player[1].state != 1 && np.count() > 0) {
			$(".autodj-next1").removeClass("disabled");
			this.nextDisabled[1] = false;
		} else if (this.nextDisabled[1] == false && player[1].state == 1) {
			$(".autodj-next1").addClass("disabled");
			this.nextDisabled[1] = true;
		}
		// disable mixnow?
		if (this.mixnowDisabled && autoplay.fading == false &&
				(
					(autoplay.nowPlaying == 0 && player[1].ready == true) ||
					(autoplay.nowPlaying == 1 && player[0].ready == true)
				)
			) {
			$(".autodj-mix").removeClass("disabled");
			this.mixnowDisabled = false;
		} else if (this.mixnowDisabled == false && autoplay.fading == true) {
			$(".autodj-mix").addClass("disabled");
			this.mixnowDisabled = true;
		}		
	}
	
}